package com.bayss.core.service.food;

import com.bayss.core.constant.ResultMsg;
import com.bayss.core.entity.foodBox.FoodBox;
import com.bayss.core.entity.foodBox.FoodBoxExample;
import com.bayss.core.entity.foodBox.mapper.FoodBoxMapper;
import com.bayss.core.entity.rbac.OBGUserInfo;
import com.bayss.core.service.user.ObgUserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Created by LDL on 2019/12/7.
 */
@Service
public class FoodBoxService {
    @Autowired
    private FoodBoxMapper foodBoxMapper;
    @Autowired
    private ObgUserService obgUserService;

    /**
     * 分页查询
     */
    public PageInfo<FoodBox> findByPage(Integer page, Integer pageSize) {
        FoodBoxExample example = new FoodBoxExample();
        example.setOrderByClause("create_time desc");
        PageHelper.startPage(page, pageSize);
        PageInfo<FoodBox> result = PageHelper.startPage(page, pageSize).doSelectPageInfo(() -> foodBoxMapper.selectByExample(example));

        for (FoodBox foodBox : result.getList()) {
            if (foodBox.getOperatorId() != null) {
                OBGUserInfo obgUserInfo = obgUserService.getUser(foodBox.getOperatorId());
                foodBox.setOperatorName(obgUserInfo == null ? "" : obgUserInfo.getNickname());
            }
        }
        return result;
    }

    /**
     * 查询所有
     *
     * @return
     */
    public List<FoodBox> findAll() {
        FoodBoxExample example = new FoodBoxExample();
        example.setOrderByClause("create_time desc");
        List<FoodBox> list = foodBoxMapper.selectByExample(example);
        return list;
    }

    /**
     * 删除
     *
     * @param id
     */
    public void deleteById(Integer id) {
        foodBoxMapper.deleteByPrimaryKey(id);
    }

    /**
     * 修改
     *
     * @param foodBox
     */
    public void update(FoodBox foodBox) {
        foodBoxMapper.updateByPrimaryKeySelective(foodBox);
    }

    /**
     * 新增
     *
     * @param foodBox
     * @return
     */
    public ResultMsg add(FoodBox foodBox) {
        if (foodBox.getBoxName() == null) {
            return ResultMsg.fail(-1, "名称不能空");
        }
        if (foodBox.getBoxPrice() == null) {
            return ResultMsg.fail(-1, "餐盒价格不能空");
        }
        if (foodBox.getCapacity() == null) {
            return ResultMsg.fail(-1, "餐盒容量不能空");
        }
        foodBox.setStatus(Byte.valueOf("1"));
        foodBox.setCreateTime(new Date());
        foodBoxMapper.insertSelective(foodBox);
        return ResultMsg.success();
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    public FoodBox getFoodBox(Integer id) {
        FoodBox foodBox = foodBoxMapper.selectByPrimaryKey(id);
        if (foodBox == null) {
            return null;
        }
        if (foodBox.getOperatorId() != null) {
            OBGUserInfo obgUserInfo = obgUserService.getUser(foodBox.getOperatorId());
            foodBox.setOperatorName(obgUserInfo == null ? "" : obgUserInfo.getNickname());
        }
        return foodBox;
    }

}
